# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: xguo

from coin.exchange.base.kr_rest.currency import NativeCurrency
from coin.exchange.bitmex.kr_rest.constants import currency_list


def validate_native_currency(currency):
  assert type(currency) == str, str(currency)
  assert currency.upper() == currency
  assert currency.upper() in currency_list, currency


def to_native_currency(currency_str):
  assert currency_str not in ('XBT')
  if currency_str == 'BTC':
    native_currency_str = 'XBT'
  elif currency_str == 'BCHABC':
    native_currency_str = 'BCH'
  else:
    native_currency_str = currency_str
  validate_native_currency(native_currency_str)
  return native_currency_str


def from_native_currency(native_currency_str):
  validate_native_currency(native_currency_str)
  if native_currency_str == 'XBT':
    currency_str = 'BTC'
  elif native_currency_str == 'BCH':
    return 'BCHABC'
  else:
    currency_str = native_currency_str
  return currency_str


class BitmexCurrency(NativeCurrency):
  _exchange = "Bitmex"

  def __init__(self, currency, native_currency):
    assert currency.upper() == currency, (currency, native_currency)
    assert native_currency.upper() == native_currency, (currency, native_currency)
    assert to_native_currency(currency) == native_currency, (currency, native_currency)
    super().__init__(currency, native_currency)

  @classmethod
  def FromStrCurrencyImpl(cls, currency_str, current_datetime):
    return BitmexCurrency(currency_str, to_native_currency(currency_str))

  @classmethod
  def FromStrNativeCurrencyImpl(cls, native_currency_str, current_datetime):
    return BitmexCurrency(from_native_currency(native_currency_str), native_currency_str)
